﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using System.Windows.Forms;
namespace testASIC
{
   public partial class Background:SerialPort
    {
        private byte[] reg_value_rw;
        private byte[] reg_value_ro;
       public int RegsNum = 256;
       public int RDLength = 258;


        private bool reg_refreshed_flag;
     
       public Background(String PortName)
       {
           reg_value_rw = new byte[48];
           reg_value_ro = new byte[RegsNum];

           //send_pair = new byte[2] { 0, 0 };
           reg_refreshed_flag = false;
           this.PortName = PortName;
           this.BaudRate = 38400;//波特率
           this.Parity = Parity.None;//无奇偶校验位
           this.StopBits = StopBits.One;//1个停止位
           this.Handshake = Handshake.None;//控制协议
           this.ReceivedBytesThreshold = RDLength;//设置 DataReceived 事件发生前内部输入缓冲区中的字节数
           this.DataReceived += new SerialDataReceivedEventHandler(serialport_DataReceived);//DataReceived事件委托
           
       }
       public byte getRegValRW(int idx)
       {
           return reg_value_rw[idx];
       }
       public byte getRegValRO(int idx)
       {
           return reg_value_ro[idx];
       }
       //public void setSendPair(byte content,int idx)
       //{
       //    send_pair[idx] = content;
       //}
        
       private void serialport_DataReceived(object sender, SerialDataReceivedEventArgs e)
       {
           string temp;
           try
           {
               StringBuilder currentline = new StringBuilder();
               //循环接收数据
               while (this.BytesToRead > 0)
               {
                   char ch = (char)this.ReadByte();
                   currentline.Append(ch);

                   
               }
               //在这里对接收到的数据进行处理
               temp = currentline.ToString();
               if (temp.Length == RDLength)
               {
                   byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(temp);
                   char[] charArray = temp.ToCharArray();
                   if ((temp[0] == '<') && (temp[RDLength-1] == '>'))
                   {
                       int ii;
                       for (ii = 0; ii < RegsNum; ii++)
                       {
                           reg_value_ro[ii] = System.Convert.ToByte(charArray[ii + 1]);
                       }
                       reg_refreshed_flag = true;

                   }
                  
               }
               currentline = new StringBuilder();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message.ToString());
           }
  
       }
        //public void SendCommand(string CommandString)
        //{
        //    byte[] WriteBuffer = Encoding.ASCII.GetBytes(CommandString);
        //    this.Write(WriteBuffer, 0, WriteBuffer.Length);

        //}



      
        public byte ASCII2HEX(byte temp)
        {
            byte returnvalu = 0;
            if ((temp >= 'A') && (temp <= 'F'))
            {
                returnvalu = (byte)(temp - 'A'+10);
            }
            else if ((temp <= '9') && (temp >= '0'))
            {
                returnvalu = (byte)(temp - '0');
            }
            return returnvalu;
        }
    }
}
